package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.DBManager;

import model.Measurement;
import model.Sensor;
import DAO.MeasurementsDao;
import DAO.SensorDao;

/**
 * Servlet implementation class WarningAlerter
 */
public class WarningAlerter extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DBManager myDb;  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WarningAlerter() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void init( ServletConfig servletconfig ) {
		// Initialisatie
		myDb = ( DBManager ) servletconfig.getServletContext().getAttribute( "dbmanager" );
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		MeasurementsDao md = new MeasurementsDao(myDb);
		ArrayList<Measurement> latestMeasurements = new ArrayList<Measurement>();
		for(int i = 1; i <= 5; i++){
			Measurement measurement = md.getLatestMeasurementByID(i);
			if(measurement != null){
				latestMeasurements.add(measurement);
			}
		}
		SensorDao sdao = new SensorDao(myDb);
		ArrayList<Sensor> sensors = sdao.getSensorsWithoutMeasurements();
		
		PrintWriter out = response.getWriter();
		boolean tempWarning = false;
		if(latestMeasurements.get(3).getValue() > sensors.get(3).getMaxValue()){
			out.println("<div id='warningbigwrapper'>");
			out.println("<div id='warningwrapperTemp'>");
			out.println("<div id='warningBackground'>");
			out.println("Waarschuwing!!!! Temperatuur is boven de "+sensors.get(3).getMaxValue()+" graden !!!!!!!!");
			out.println("</div>");
			out.println("</div>");
			tempWarning = true;
		}if(latestMeasurements.get(4).getValue() > sensors.get(4).getMaxValue()){
			if(tempWarning){
				out.println("<div id='warningwrapperLucht'>");
				out.println("<div id='warningBackground'>");
				out.println("Waarschuwing!!!! Luchtvochtigheid is boven de "+sensors.get(4).getMaxValue() +" procent!!!!!");
			}else{
				out.println("<div id='warningbigwrapper'>");
				out.println("<div id='warningwrapperLucht' style='padding-top:30px;'>");
				out.println("<div id='warningBackground'>");
				out.println("Waarschuwing!!!! Luchtvochtigheid is boven de "+sensors.get(4).getMaxValue() +" procent!!!!!");
			}
			out.println("</div>");
			out.println("</div>");
		}
		out.println("</div>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
